using System;
using System.IO;
using System.Text;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.Test.BO;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using NUnit.Framework;
using Rhino.Mocks;
using XmlLoader=FireStarterModeller.Logic.XmlLoader;

namespace FireStarterModeller.Test.Logic
{
    [TestFixture]
    public class TestProjectLoader
    {
        #region Setup/Teardown
        [TestFixtureSetUp]
        public virtual void TestFixtureSetup()
        {
            TestUtils.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            TestUtils.WaitForGC();
        }

        [TearDown]
        public virtual void TearDownTest()
        {
        }
        #endregion

        private static string CreateProjectXml(DMSolution dmSolution)
        {
            StringBuilder stringBuilder = new StringBuilder();
            ProjectPersistor projectPersistor = new ProjectPersistor(new XmlPersistor(new StringWriter(stringBuilder)));
            projectPersistor.AddProject(dmSolution);
            projectPersistor.Persist();
            return stringBuilder.ToString();
        }

        [Test]
        public void Test_Load_CallsThrough()
        {
            //---------------Set up test pack-------------------
            MockRepository mock = new MockRepository();
            IBoLoader boLoader = mock.StrictMock<IBoLoader>();
            ProjectLoader projectLoader = new ProjectLoader(boLoader);
            //-------------Assert Preconditions -------------
            Expect.Call(boLoader.Load).Repeat.Once();
            mock.ReplayAll();
            //---------------Execute Test ----------------------
            projectLoader.Load();
            //---------------Test Result -----------------------
            mock.VerifyAll();
        }

        [Test]
        public void Test_BoLoader_ReturnsConstructedInstance()
        {
            //---------------Set up test pack-------------------
            MockRepository mock = new MockRepository();
            IBoLoader boLoader = mock.StrictMock<IBoLoader>();
            //-------------Assert Preconditions -------------
            mock.ReplayAll();
            //---------------Execute Test ----------------------
            ProjectLoader projectLoader = new ProjectLoader(boLoader);
            //---------------Test Result -----------------------
            mock.VerifyAll();
            Assert.AreSame(boLoader, projectLoader.BoLoader);
        }

        [Test]
        public void Test_Load_ProjectWithNoBos()
        {
            //---------------Set up test pack-------------------
            DMSolution dmSolution = SharedTestUtils.CreateSavedDMSolution();
            string xml = CreateProjectXml(dmSolution);
            BusinessObjectManager.Instance.ClearLoadedObjects();

            XmlLoader xmlLoader = new XmlLoader(new StringReader(xml));
            ProjectLoader projectLoader = new ProjectLoader(xmlLoader);
            //-------------Assert Preconditions -------------
            Assert.IsNull(projectLoader.Solution);
            //---------------Execute Test ----------------------
            projectLoader.Load();
            //---------------Test Result -----------------------
            Assert.IsNotNull(projectLoader.Solution);
            Assert.AreEqual(dmSolution.SolutionID, projectLoader.Solution.SolutionID);
        }

        [Test]
        public void Test_Load_ProjectWithOneBo()
        {
            //---------------Set up test pack-------------------
            DMSolution dmSolution = SharedTestUtils.CreateSavedDMSolution();
            DMClass dmClass = SharedTestUtils.CreateSavedDMClass(dmSolution.DefaultAssembly);
            string xml = CreateProjectXml(dmSolution);
            
            BusinessObjectManager.Instance.ClearLoadedObjects();
            BORegistry.DataAccessor = new DataAccessorInMemory();

            XmlLoader xmlLoader = new XmlLoader(new StringReader(xml));
            ProjectLoader projectLoader = new ProjectLoader(xmlLoader);
            //-------------Assert Preconditions -------------
            Assert.IsNull(projectLoader.Solution);
            //---------------Execute Test ----------------------
            projectLoader.Load();
            //---------------Test Result -----------------------
            Assert.IsNotNull(projectLoader.Solution);
            Assert.AreEqual(dmSolution.SolutionID, projectLoader.Solution.SolutionID);
            Assert.AreEqual(1, projectLoader.Solution.AllClasses.Count);
            DMClass loadedDMClass = projectLoader.Solution.AllClasses[0];
            Assert.AreNotSame(dmClass, loadedDMClass);
            Assert.AreEqual(dmClass.ClassID, loadedDMClass.ClassID);
        }
       
    }
}
